package com.coocaa.ops.api.v1.internal.provider.impl;

import com.coocaa.ops.api.base.cache.enums.InterfaceCacheMethodEnum;
import com.coocaa.ops.api.base.cache.model.SimpleCacheKey;
import com.coocaa.ops.api.base.cache.service.SimpleCacheService;
import com.coocaa.ops.api.common.constant.PolicyGroupCategory;
import com.coocaa.ops.api.v1.home.biz.IHomepageBiz;
import com.coocaa.ops.api.v1.home.model.query.HomeQueryModel;
import com.coocaa.ops.api.v1.internal.provider.ICacheUpdateProvider;
import com.coocaa.ops.common.core.biz.cache.constant.CacheTime;
import com.coocaa.ops.common.core.biz.homepage.entity.HomeModuleContentConfigInfo;
import com.coocaa.ops.common.core.biz.homepage.entity.HomepageInfoEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @author tangminghong
 * @date 2020-08-11
 */
@Service("homepage")
public class HomepageCacheUpdateProviderImpl implements ICacheUpdateProvider {

    @Autowired
    private SimpleCacheService cacheService;
    @Autowired
    private IHomepageBiz homepageBiz;


    /**
     * 更新缓存-通过资源ID
     *
     * @param idStr
     * @return 缓存key列表
     */
    @Override
    public List<String> updateCacheByResourceId(String idStr) {
        Integer id = Integer.valueOf(idStr);
        HomeQueryModel queryModel = getHomeQueryModel(id);
        List<String> finalCacheKeys = new ArrayList<>(2);
        HomepageInfoEntity homepage = homepageBiz.getHomepageAndModuleList(queryModel);
        if (homepage != null) {
            SimpleCacheKey cacheKey = getHomeCacheKey(id);
            cacheService.set(cacheKey, homepage, CacheTime.ONE_DAY);
            finalCacheKeys.add(cacheKey.toString());
        }
        handleModuleDmp(queryModel, finalCacheKeys);
        return finalCacheKeys;
    }

    private HomeQueryModel getHomeQueryModel(Integer homepageId) {
        HomeQueryModel homeQueryModel = new HomeQueryModel();
        homeQueryModel.setHomepageId(homepageId);
        homeQueryModel.setPolicyCategory(PolicyGroupCategory.FORMAL);
        return homeQueryModel;
    }

    private SimpleCacheKey getHomeCacheKey(Integer homepageId) {
        SimpleCacheKey cacheKey = SimpleCacheKey.getCacheKeyInstance(InterfaceCacheMethodEnum.getHomepageAndModuleList);
        cacheKey.withFilterKey("homeId", homepageId);
        return cacheKey;
    }

    private SimpleCacheKey getModuleDmpCacheKey(Integer homepageId) {
        SimpleCacheKey cacheKey = SimpleCacheKey.getCacheKeyInstance(InterfaceCacheMethodEnum.getDmpModuleList);
        cacheKey.withFilterKey("homeId", homepageId);
        return cacheKey;
    }

    private void handleModuleDmp(HomeQueryModel queryModel, List<String> cacheKeys) {
        List<HomeModuleContentConfigInfo> moduleList = homepageBiz.getDmpModuleList(queryModel);
        if (moduleList != null) {
            SimpleCacheKey cacheKey = getModuleDmpCacheKey(queryModel.getHomepageId());
            cacheService.set(cacheKey, moduleList, CacheTime.ONE_DAY);
            cacheKeys.add(cacheKey.toString());
        }
    }
}
